<?php

namespace api\controller;

// +----------------------------------------------------------------------
// | 工序分类数据控制层
// +----------------------------------------------------------------------

use common\controller\VerifyController;
use common\service\StepWorkflowService;
use think\Request;

class StepWorkflowController extends VerifyController
{
    /**
     * @var \common\service\StepWorkflowService
     */
    protected $stepWorkFlowService;

    public function __construct()
    {
        parent::__construct();
        $this->stepWorkFlowService = StepWorkflowService::getInstance();
    }


    /**
     * 自动检查工序流数据
     * @param Request $request
     * @return \think\Response
     */
    public function autoFixConfig(Request $request)
    {
        $res = $this->stepWorkFlowService->autoFixConfig();
        return json(success_response('', $res));
    }

    /**
     * 通过工序id查询工序流
     * @param Request $request
     * @return \think\Response
     */
    public function selectByStepId(Request $request)
    {
        $res = $this->stepWorkFlowService->selectByStepId($request->param());
        return json(success_response('', $res));
    }

    /**
     * 查询工序标准目录
     * @param Request $request
     * @return \think\Response
     */
    public function getWorkflowStepStandards(Request $request)
    {
        $res = $this->stepWorkFlowService->getWorkflowStepStandards($request->param()['param']['project_template_id'] ?? 0);
        return json(success_response('', $res));
    }

    /**
     * 获取实体工序流
     * @param Request $request
     * @return \think\Response|void
     */
    public function getEntityWorkFlowId(Request $request)
    {
        try {
            $projectId = $request->param()['param']['project_id'] ?? 0;
            $stepWorkFlowCode = $request->param()['param']['step_work_flow_code'] ?? 0;
            $res = $this->stepWorkFlowService->getEntityWorkFlowId($projectId, $stepWorkFlowCode);
            return json(success_response('', $res));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 获取已开启的工序流提出阶段
     * @param Request $request
     * @return \think\Response
     */
    public function getEnableStepWorkflowStageList(Request $request)
    {
        $param = $request->param()['param'];
        $projectId = $param['filter']['project_id'];
        $page = $param['page'] ?? [1, C('default_page_total')];
        $episodeCode = $param['filter']['episode_code'] ?? '';
        $moduleCode = $param['filter']['module_code'] ?? '';
        $reviewFeedbackId = $param['filter']['review_feedback_id'] ?? 0;

        $res = $this->stepWorkFlowService->getEnableStepWorkflowStageList($projectId, $page, $episodeCode, $moduleCode, $reviewFeedbackId);
        return json(success_response('', $res));
    }
}
